<template>
  <div>
    <div v-if="node.type != 'route'" class="designer">
      <div class="node">
        <div class="title" :class="{ 'a': node.type == 'approval'}">
          <span v-if="node.type == 'start'" class="iconfont icon_t"><img src="../assets/person.png" alt=""></span>
          <span v-if="node.type == 'start'">{{ node.name }}</span>
          <!-- 添加审批人节点-->
          <template v-else-if="node.type == 'approval'">
            <span class="iconfont"></span>
            <span v-if="!node.nameEdit" class="editable-title" @click="editTitle(node)">{{ node.name }}</span>
            <input
              v-else
              v-model="node.name"
              v-focus
              type="text"
              class="ant-input editable-title-input"
              :placeholder="node.name"
              @blur="editTitle(node)"
              @focus="$event.currentTarget.select()"
            >
            <i class="anticon anticon-close close" @click="delDesigner(node)" />
          </template>
        </div>
        <div v-if="node&&node.type != 'start'" class="content" @click="openDrawer(node)">
          <div class="text" :class="{ 'placeholder': node.propertys&&node.propertys.type === 'manual' && !node.propertys.operaters }">
            {{ getTitle(node) || node.placeholder }}
          </div>
          <i class="anticon anticon-right arrow" />
        </div>
        <div v-else class="content">
          <div class="text">
            所有人
          </div>
        </div>
      </div>
      <addNode :node-id="node.nodeId" :child-node.sync="node.childNode" />
    </div>
    <div v-else class="branch">
      <div class="box">
        <button class="add" @click="addJudge(node)">添加条件</button>
        <div v-for="(item, index) in node.conditionNodes" :key="index" class="item">
          <div class="node">
            <div class="nodebox">
              <div class="judge">
                <div v-if="index > 0 && !item.default" class="sort-left" @click="upNext(0, index, node)">
                  <svg
                    class="svg-icon left"
                    viewBox="0 0 1024 1024"
                    xmlns="http://www.w3.org/2000/svg"
                    width="128"
                    height="128"
                  >
                    <defs />
                    <path
                      d="M403.046 529.203l373.863-373.76a27.955 27.955 0 000-39.629l-39.527-39.628a27.955 27.955 0 00-39.628 0L264.602 509.338a27.955 27.955 0 000 39.628l433.152 433.152a27.814 27.814 0 0039.628 0l39.527-39.526a27.955 27.955 0 000-39.526l-373.76-373.863z"
                    />
                  </svg>
                </div>
                <div class="title">
                  <span class="iconfont icon_t"><img src="../assets/sx.png" alt=""></span>
                  <span
                    v-if="!item.nameEdit"
                    :class="{ 'editable': !item.default }"
                    style="display: inline-block;overflow: hidden;"
                    @click="editTitle(item)"
                  >{{ item.name }}</span>
                  <input
                    v-else
                    v-model="item.name"
                    v-focus
                    type="text"
                    class="ant-input editable-title-input"
                    :placeholder="item.name"
                    @blur="editTitle(item)"
                    @focus="$event.currentTarget.select()"
                  >
                  <span class="priority">优先级{{ index + 1 }}</span>
                  <i
                    v-if="!item.nameEdit"
                    class="anticon anticon-close close"
                    @click="delJudge(index, node, item.default)"
                  />
                </div>
                <div
                  v-if="index < node.conditionNodes.length - isDefault() && !item.default"
                  class="sort-right"
                  @click="upNext(1, index, node)"
                >
                  <svg
                    class="svg-icon right"
                    viewBox="0 0 1024 1024"
                    xmlns="http://www.w3.org/2000/svg"
                    width="128"
                    height="128"
                  >
                    <defs />
                    <path
                      d="M638.362 529.203l-373.76-373.76a27.955 27.955 0 010-39.629l39.526-39.628a27.955 27.955 0 0139.629 0l433.152 433.152a27.955 27.955 0 010 39.628L343.757 982.118a27.955 27.955 0 01-39.629 0l-39.629-39.526a27.955 27.955 0 010-39.526l373.863-373.863z"
                    />
                  </svg>
                </div>
                <div
                  v-if="!item.default"
                  class="content"
                  :class="{ 'placeholder': item.conditions.length == 0 || (item.conditions.length == 1 && !item.conditions[0].length) }"
                  @click="openDrawer(item)"
                >
                  <el-tooltip
                    v-if="item.conditions.length > 1 || (item.conditions.length == 1 && item.conditions[0].length)"
                    effect="dark"
                    placement="left"
                  >
                    <div slot="content" v-html="getText(item.conditions)" />
                    <span>已设置 <i class="el-icon-warning-outline" /></span>
                  </el-tooltip>
                  <span v-else>{{ item.placeholder }}</span>
                </div>
                <div v-else class="content">{{ item.placeholder }}</div>
              </div>
              <addNode :node-id="item.nodeId" :child-node.sync="item.childNode" />
            </div>
          </div>
          <flowpathDesigner
            v-if="item.childNode"
            :node.sync="item.childNode"
            :get-conditions-text="getConditionsText"
            @opend="openDrawer"
          />
          <template v-if="index == 0">
            <div class="top-left-cover-line" />
            <div class="bottom-left-cover-line" />
          </template>
          <template v-if="index == node.conditionNodes.length - 1">
            <div class="top-right-cover-line" />
            <div class="bottom-right-cover-line" />
          </template>
        </div>
      </div>
      <addNode :node-id="node.nodeId" :child-node.sync="node.childNode" />
    </div>
    <flowpathDesigner
      v-if="node.childNode"
      :node.sync="node.childNode"
      :get-conditions-text="getConditionsText"
      @opend="openDrawer"
    />
  </div>
</template>
<script>
import addNode from './addNode.vue'
import { addCondition } from './workflow'
export default {
  name: 'ApproveFlowPathDesigner',
  components: { addNode },
  directives: {
    focus: {
      inserted(el) {
        el.focus()
      }
    }
  },
  props: ['node', 'getConditionsText'],
  data() {
    return {}
  },
  computed: {},
  watch: {},
  mounted() { },
  methods: {
    getTitle(n) {
      console.log('输出n.propertys.operaters：')
      console.log(n.propertys)
      if (n.propertys.type === 'manual') {
        if (!n.propertys.operaters) {
          return ''
        }
        // 后续根据 n.propertys.operaterType(审批类型) n.propertys.operaters（审批人） 获取到用户、角色、主管等名称
        return n.propertys.operaters
      }
      if (n.propertys.type === 'autoAgree') {
        return '自动通过'
      }
      return '自动拒绝'
    },
    // 转义中文
    getText(list) {
      return this.getConditionsText(list)
    },
    // 判断是否存在默认分支
    isDefault() {
      return this.node.conditionNodes.filter((item) => item.default).length
        ? 2
        : 1
    },
    // 编辑标题
    editTitle(n) {
      if (n.default) {
        return
      }
      n.nameEdit = !n.nameEdit
    },
    // 条件交换位置
    upNext(i, y, n) {
      let x = y
      if (i) {
        x++
      } else {
        x--
      }
      n.conditionNodes[y] = n.conditionNodes.splice(
        x,
        1,
        n.conditionNodes[y]
      )[0]
    },
    // 其他节点删除
    delDesigner(n) {
      this.$emit('update:node', n.childNode)
    },
    // 条件节点删除
    delJudge(i, n, _default = false) {
      const _del = () => {
        if (n.conditionNodes.length > 2) {
          n.conditionNodes.splice(i, 1)
          return
        }
        this.$emit('update:node', n.childNode)
      }
      if (_default) {
        this.$confirm(
          '当未满足其他条件时，均进入此条件分支，删除后条件分支可能不完整而导致无法提交审批单',
          '确定删除？',
          {
            confirmButtonText: '删除',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )
          .then(() => {
            _del()
          })
          .catch(() => { })
        return
      }
      _del()
    },
    // 条件节点添加
    addJudge(n) {
      n.conditionNodes.splice(
        n.conditionNodes.length - 1,
        0,
        addCondition(n.conditionNodes.length, n.nodeId)
      )
    },
    // 打开设置
    openDrawer(n) {
      this.$emit('opend', n)
    }
  }
}
</script>
<style lang="scss" scoped>
.top-left-cover-line {
  left: -1px;
}

.top-left-cover-line,
.top-right-cover-line {
  position: absolute;
  height: 8px;
  width: 50%;
  background-color: #f5f5f7;
  top: -4px;
}

.top-right-cover-line {
  right: -1px;
}

.bottom-left-cover-line {
  left: -1px;
}

.bottom-left-cover-line,
.bottom-right-cover-line {
  position: absolute;
  height: 8px;
  width: 50%;
  background-color: #f5f5f7;
  bottom: -4px;
}

.icon_t {
  width: 15px;
  float: left;
  display: inherit;
  left: -4px;
  top: 0px !important;
  img{
    width: 100%;
  }
}

.bottom-right-cover-line {
  right: -1px;
}

@font-face {
  font-family: "anticon";
  font-display: fallback;
  src: url("../assets/font_148784_v4ggb6wrjmkotj4i.eot");
  src: url("../assets/font_148784_v4ggb6wrjmkotj4i.woff") format("woff"),
    url("../assets/font_148784_v4ggb6wrjmkotj4i.ttf") format("truetype"),
    url("../assets/font_148784_v4ggb6wrjmkotj4i.svg#iconfont") format("svg");
}

.anticon {
  display: inline-block;
  font-style: normal;
  vertical-align: baseline;
  text-align: center;
  text-transform: none;
  line-height: 1;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.anticon:before {
  display: block;
  font-family: anticon !important;
}

.anticon-close:before {
  content: "\E633";
}

.anticon-right:before {
  content: "\E61F";
}

.anticon-exclamation-circle {
  color: rgb(242, 86, 67);
}

.anticon-exclamation-circle:before {
  content: "\E62C";
}

.anticon-left:before {
  content: "\E620";
}

.anticon-close-circle:before {
  content: "\E62E";
}

.ant-input {
  font-family: Chinese Quote, -apple-system, BlinkMacSystemFont, Segoe UI,
    PingFang SC, Hiragino Sans GB, Microsoft YaHei, Helvetica Neue, Helvetica,
    Arial, sans-serif;
  font-variant: tabular-nums;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  list-style: none;
  position: relative;
  display: inline-block;
  padding: 4px 11px;
  width: 100%;
  height: 32px;
  font-size: 14px;
  line-height: 1.5;
  color: rgba(0, 0, 0, 0.65);
  background-color: #fff;
  background-image: none;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  transition: all 0.3s;
}

.ant-input::-moz-placeholder {
  color: #bfbfbf;
  opacity: 1;
}

.ant-input:-ms-input-placeholder {
  color: #bfbfbf;
}

.ant-input::-webkit-input-placeholder {
  color: #bfbfbf;
}

.ant-input:focus,
.ant-input:hover {
  border-color: #40a9ff;
  border-right-width: 1px !important;
}

.ant-input:focus {
  outline: 0;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
}

.designer {
  display: -webkit-inline-box;
  display: -ms-inline-flexbox;
  display: inline-flex;
  width: 100%;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -ms-flex-direction: column;
  flex-direction: column;
  -webkit-box-pack: start;
  -ms-flex-pack: start;
  justify-content: flex-start;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  -webkit-box-flex: 1;
  -ms-flex-positive: 1;
  // padding: 0 50px;
  position: relative;

  .node {
    display: -webkit-inline-box;
    display: -ms-inline-flexbox;
    display: inline-flex;
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    -ms-flex-direction: column;
    flex-direction: column;
    position: relative;
    width: 220px;
    min-height: 72px;
    -ms-flex-negative: 0;
    flex-shrink: 0;
    background: #fff;
    border-radius: 4px;
    cursor: pointer;

    .title {
      position: relative;
      display: -webkit-box;
      display: -ms-flexbox;
      display: flex;
      -webkit-box-align: center;
      -ms-flex-align: center;
      align-items: center;
      padding-left: 16px;
      padding-right: 30px;
      // width: 100%;
      height: 24px;
      line-height: 24px;
      font-size: 12px;
      color: #fff;
      text-align: left;
      background: #576a95;
      border-radius: 4px 4px 0 0;

      .iconfont {
        font-family: "iconfont" !important;
        font-size: 12px;
        margin-right: 5px;
      }
    }

    .title.a {
      background: rgb(255, 148, 62);
    }

    .title.b {
      background: rgb(50, 150, 250);
    }
    .title.c {
      background: rgb(128, 128,0);
    }
    .editable-title {
      line-height: 15px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      border-bottom: 1px dashed transparent;
    }

    .editable-title:before {
      content: "";
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      right: 40px;
    }

    .editable-title:hover {
      border-bottom: 1px dashed #fff;
    }

    .editable-title-input {
      flex: none;
      height: 18px;
      padding-left: 4px;
      text-indent: 0;
      font-size: 12px;
      line-height: 18px;
      z-index: 1;
    }

    .editable-title-input:hover {
      text-decoration: none;
    }

    .close {
      display: none;
      position: absolute;
      right: 10px;
      top: 50%;
      transform: translateY(-50%);
      width: 20px;
      height: 20px;
      font-size: 14px;
      color: #fff;
      border-radius: 50%;
      text-align: center;
      line-height: 20px;
    }

    .content {
      position: relative;
      font-size: 14px;
      padding: 16px;
      padding-right: 30px;

      .text {
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 3;
        -webkit-box-orient: vertical;
      }

      .arrow {
        position: absolute;
        right: 10px;
        top: 50%;
        -webkit-transform: translateY(-50%);
        transform: translateY(-50%);
        width: 20px;
        height: 14px;
        font-size: 14px;
        color: #979797;
      }

      .anticon-right:before {
        content: "\E61F";
      }

      .anticon:before {
        display: block;
        font-family: anticon !important;
      }
    }
  }

  .node:after {
    pointer-events: none;
    content: "";
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 2;
    border-radius: 4px;
    border: 1px solid transparent;
    -webkit-transition: all 0.1s cubic-bezier(0.645, 0.045, 0.355, 1);
    transition: all 0.1s cubic-bezier(0.645, 0.045, 0.355, 1);
    -webkit-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.1);
    box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.1);
  }

  .node.active:after,
  .node:active:after,
  .node:hover:after {
    border: 1px solid #3296fa;
    box-shadow: 0 0 6px 0 rgba(50, 150, 250, 0.3);
  }

  .node.active .close,
  .node:active .close,
  .node:hover .close {
    display: block;
  }
}

.branch {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -ms-flex-direction: column;
  flex-direction: column;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  min-height: 270px;
  width: 100%;
  -ms-flex-negative: 0;
  flex-shrink: 0;

  .box {
    display: flex;
    overflow: visible;
    min-height: 180px;
    height: auto;
    border-bottom: 2px solid #ccc;
    border-top: 2px solid #ccc;
    position: relative;
    margin-top: 15px;

    .item {
      display: inline-flex;
      -webkit-box-orient: vertical;
      -webkit-box-direction: normal;
      flex-direction: column;
      -webkit-box-align: center;
      align-items: center;
      position: relative;
      background: #f5f5f7;
    }

    .item:before {
      content: "";
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      z-index: 0;
      margin: auto;
      width: 2px;
      height: 100%;
      background-color: #cacaca;
    }

    .add {
      border: none;
      outline: none;
      user-select: none;
      justify-content: center;
      font-size: 12px;
      padding: 0 10px;
      height: 30px;
      line-height: 30px;
      border-radius: 15px;
      color: #3296fa;
      background: #fff;
      box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
      position: absolute;
      top: -16px;
      left: 50%;
      transform: translateX(-50%);
      transform-origin: center center;
      cursor: pointer;
      z-index: 1;
      display: inline-flex;
      align-items: center;
      -webkit-transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
      transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
    }

    .add:hover {
      transform: translateX(-50%) scale(1.1);
      box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.1);
    }

    .add:active {
      transform: translateX(-50%);
      box-shadow: none;
    }

    .node,
    .nodebox {
      display: inline-flex;
      -webkit-box-orient: vertical;
      -webkit-box-direction: normal;
      flex-direction: column;
      -webkit-box-flex: 1;
    }

    .nodebox {
      padding-top: 30px;
      padding-right: 50px;
      padding-left: 50px;
      -webkit-box-pack: center;
      justify-content: center;
      -webkit-box-align: center;
      align-items: center;
      flex-grow: 1;
      position: relative;

      .judge:hover .editable,
      .node-wrap-box:hover .editable {
        border-bottom: 1px dashed #fff;
      }

      .judge:hover .editable.editing,
      .node-wrap-box:hover .editable.editing {
        text-decoration: none;
        border: 1px solid #d9d9d9;
      }

      .judge:hover .editable {
        border-color: #15bc83;
      }

      .editable {
        line-height: 15px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        border-bottom: 1px dashed transparent;
      }

      .editable:before {
        content: "";
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        right: 40px;
      }

      .editable:hover {
        border-bottom: 1px dashed #fff;
      }

      .editable-title-input {
        flex: none;
        height: 18px;
        padding-left: 4px;
        text-indent: 0;
        font-size: 12px;
        line-height: 18px;
        margin-bottom: 2px;
        z-index: 1;
        width: 60%;
      }

      .editable-title-input:hover {
        text-decoration: none;
      }

      .judge {
        position: relative;
        width: 180px;
        background: #fff;
        border-radius: 4px;
        // padding: 14px 19px;
        cursor: pointer;

        .title {
          position: relative;
          font-size: 12px;
          // color: rgba(25, 31, 37, 0.56);
          text-align: left;
          line-height: 16px;
          padding: 7px 19px 0px;
          color: rgb(255, 255, 255);
          background-color: rgb(60, 179, 113);
          border-top-left-radius: 5px;
          border-top-right-radius: 5px;

          .iconfont {
            font-family: "iconfont" !important;
            font-size: 12px;
            margin-right: 5px;
            position: relative;
            top: -4px;
          }

          .editable {
            display: inline-block;
            max-width: 120px;
            // color: #15bc83;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
          }

          .priority {
            display: inline-block;
            float: right;
            margin-right: 10px;
            // color: rgba(25, 31, 37, 0.56);
            color: #fff;
          }
        }

        .close {
          display: none;
          position: absolute;
          right: 4px;
          top: 0px;
          width: 20px;
          height: 20px;
          font-size: 14px;
          color: #fff;
          border-radius: 50%;
          text-align: center;
          line-height: 20px;
          z-index: 2;
        }

        .content {
          font-size: 14px;
          color: #191f25;
          text-align: left;
          margin-top: 6px;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 3;
          -webkit-box-orient: vertical;
          padding: 7px 19px 14px;
        }

        .sort-left,
        .sort-right {
          position: absolute;
          top: 0;
          bottom: 0;
          display: none;
          z-index: 1;

          svg {
            width: 12px;
            height: 12px;
          }

          svg:not(:root) {
            overflow: hidden;
          }
        }

        .sort-left {
          left: 0;
          border-right: 1px solid #f6f6f6;
        }

        .sort-right {
          right: 0;
          border-left: 1px solid #f6f6f6;
        }

        .sort-left:hover,
        .sort-right:hover {
          background: #efefef;
        }
      }

      .judge:after {
        pointer-events: none;
        content: "";
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 2;
        border-radius: 4px;
        border: 1px solid transparent;
        transition: all 0.1s cubic-bezier(0.645, 0.045, 0.355, 1);
        box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.1);
      }

      .judge.active:after,
      .judge:active:after,
      .judge:hover:after {
        border: 1px solid #3296fa;
        box-shadow: 0 0 6px 0 rgba(50, 150, 250, 0.3);
      }

      .judge.active .close,
      .judge:active .close,
      .judge:hover .close {
        display: block;
      }

      .judge:hover .priority {
        display: none;
      }

      .judge.error:after {
        border: 1px solid #f25643;
        box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.1);
      }

      .judge:hover .sort-left,
      .judge:hover .sort-right {
        display: flex;
        align-items: center;
      }
    }

    .nodebox:before {
      content: "";
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      margin: auto;
      width: 2px;
      height: 100%;
      background-color: #cacaca;
    }
  }
}

.placeholder {
  color: #bfbfbf !important;
}</style>
